jQuery(function(){
	
	///获得投标记录的列表
	 var recordData = {};
	 var bidId=jQuery('#bidId').val();
	 recordData.bidId=bidId;
	 var bidRecordTable;
	    jQuery(document).ready(function () {
	    	bidRecordTable = jQuery('#bidRecordTable').DataTable({
	            "serverSide": true,
	            "iDisplayLength":5,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/bid/getbidrecord',
	                "data": function (d) {
	                    return $.extend({}, d, recordData);
	                }
	            },
	            columns: [
	                {
	                    data: null,
	                    defaultContent: ""//排序
	                },
	                {
	                	data: null,
	    	            defaultContent: "",
	    	            render: function (data, type, full, meta) {
	    	            	var id=data.id
	    	            	var operate ='<input type="checkbox" class="checkchild">';
	    	            	return operate;
	    	            }   
	                },
	                {
	                    data: 'bidUnit'
	                },
	                {
	                    data: 'price'
	                },
	                {
						data : 'deliveryDate',
						render : function(data,type, full,meta) {
							    if (data) {
								return moment(data).format("YYYY-MM-DD");
							}
						}
					},
	                {
	                    data: 'other'
	                },	             
	                {
	                    data: 'winner'
	                }
	                ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	            }
	        });
	    	 
	    });
	    
//新建投标情况
		jQuery('.addBtn').on('click', function () {
			jQuery('#id').val('');
			jQuery('#bUnit').val('');
			jQuery('#price').val('');
			jQuery('#deliveryDate').val(moment(new Date()).format("YYYY-MM-DD"));
			jQuery('#other').val('');
			jQuery('#bidRecordModal').modal('show');
	    });
	
////删除投标情况记录
	    jQuery('.delBtn').on('click', function () {
	 	   var selects=getSelect("#bidRecordTable",1);
	 	   var url=window.rootPath+"/ui/bid/delBidRecord";
	 	   delSelect(selects,url,bidRecordTable) 
	 	});   
//////投标情况编辑
	    jQuery('.editBtn').on('click', function () {
	       var selects=getSelect("#bidRecordTable",1);
		   if(selects.length>1){
			   $.springLemonDialog({
			         content: '只能编辑一条记录',// 确认文本
			         clickButton: function (sender, modal, index) {// 按钮事件处理
			             $(this).closeDialog(modal);
			         }
			   }); 
		   }
		   else if(selects.length==0){
			   $.springLemonDialog({
			         content: '请选择要编辑的记录',// 确认文本
			         clickButton: function (sender, modal, index) {// 按钮事件处理
			             $(this).closeDialog(modal);
			         }
			   }); 
		   }else if(selects.length==1){
			   jQuery('#id').val(selects[0].id);
			   jQuery('#bUnit').val(selects[0].bidUnit);
			   jQuery('#price').val(selects[0].price);
			   jQuery('#deliveryDate').val(moment(selects[0].deliveryDate).format("YYYY-MM-DD"));
			   jQuery('#other').val(selects[0].other);
			   jQuery('#winner').val(selects[0].winner);
			   jQuery('#bidRecordModal').modal('show');
		   }
		   
		});	
////保存投标情况记录
	jQuery('.saveBtn').on('click',function(){
		if (jQuery("#bidRecordForm") && !jQuery("#bidRecordForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		var id = jQuery("#id").val();
		var bidId=jQuery('#bidId').val();
		var bUnit = jQuery('#bUnit').val();
		var price = jQuery('#price').val();
		var deliveryDate = jQuery('#deliveryDate').val();
		var other = jQuery("#other").val();
		var winner = jQuery("#winner").val();
		// 判断是编辑还是新增
		if (id == "") {
			var url = window.rootPath + "/ui/bid/saveRecord";
			var d = {
				"bidUnit" : bUnit,
				"bid":{
					"id": bidId
				},
				"price" : price,
				"deliveryDate" : deliveryDate,
				"other" : other,
				"winner" : winner
			};

			jQuery.ajax({
				type : 'post',
				url : encodeURI(url),
				data : JSON.stringify(d),
				dataType : 'json',
				contentType : 'application/json;charset=utf-8',
				success : function(data) {
					jQuery('#bidRecordModal').modal('hide');
					bidRecordTable.draw();
				}
			});

		} else {
			var data = {
				"id" : id,
				"bidUnit" : bUnit,
				"price" : price,
				"deliveryDate" : deliveryDate,
				"other" : other,
				"winner" : winner
			};
			var url = window.rootPath + '/api/bidRecords/' + data.id;
			jQuery(".btn").attr("disabled", true);
			jQuery.ajax({
				url : encodeURI(url),
				type : 'patch',
				data : JSON.stringify(data),
				dataType : 'json',
				contentType : 'application/json;charset=utf-8',
				success : function() {
					jQuery(".btn").attr("disabled", false);
					jQuery('#bidRecordModal').modal('hide');
					bidRecordTable.draw(false);
				}

			});
		}
	})    
/////保存投标反馈
	jQuery('.saveBackBtn').on('click',function(){
		if (jQuery("#bidBackForm") && !jQuery("#bidBackForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		var id=jQuery("#bidId").val();
		var bidDate=jQuery('#bidDate').val();
		var backType=jQuery('#backType').val() ;
		var deviceAcount=jQuery("#deviceAcount").val();
		var selfDevicePrice=jQuery('#selfDevicePrice').val();
		var buyDevicePrice=jQuery('#buyDevicePrice').val();
		var payType=jQuery('#payType').val();
		var summary=jQuery('#summary').val();
		var url = window.rootPath + '/api/bids/'+ id;
	    var d = {
	     	            "bidDate": bidDate,
	     	            "backType": backType,
	     	            "deviceAcount": deviceAcount,
	     	            "selfDevicePrice": selfDevicePrice,
	     	            "buyDevicePrice": buyDevicePrice,
	     	            "payType": payType,
	     	            "summary": summary    
	     	        };
	      jQuery(".btn").attr("disabled", true);
		  jQuery.ajax({
	          type: 'patch',
	          url: encodeURI(url),
	          data: JSON.stringify(d),
	          dataType: 'json',
	          contentType: 'application/json;charset=utf-8',
	          success: function (data) {
	        	  jQuery(".btn").attr("disabled", false);
	        	  window.location = window.rootPath+"/ui/bid"; 	  
	          }
 	  	});	
    })	
	
    ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});

	
	
})
